package com.xucy.springboot.redis.PubSub;

import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.listener.PatternTopic;
import org.springframework.data.redis.listener.RedisMessageListenerContainer;
import org.springframework.data.redis.listener.adapter.MessageListenerAdapter;

/**
 * @Author xucy
 * @Date 2019-07-04 14:34
 * @Description 发布订阅配置类，配置哪个类是对哪个主题的订阅
 **/

@Configuration
@AutoConfigureAfter(SubController.class)
public class PubSubConfig {

    @Bean
    public MessageListenerAdapter getMessageListenerAdapter(SubController subController){
        return new MessageListenerAdapter(subController);
    }


    @Bean
    public RedisMessageListenerContainer getRedisMessageListenerContainer(RedisConnectionFactory redisConnectionFactory,MessageListenerAdapter messageListenerAdapter){
        RedisMessageListenerContainer redisMessageListenerContainer=new RedisMessageListenerContainer();
        redisMessageListenerContainer.setConnectionFactory(redisConnectionFactory);
        redisMessageListenerContainer.addMessageListener(messageListenerAdapter, new PatternTopic("pub"));
        return redisMessageListenerContainer;
    }
}
